<?php
// +----------------------------------------------------------------------
// | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2019 http://www.thinkcmf.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 老猫 <thinkcmf@126.com>
// +----------------------------------------------------------------------
namespace app\portal\controller;

use cmf\controller\HomeBaseController;
use app\portal\service\PostService;
use api\mall\model\CultureModel;
use app\user\model\UserModel;
use api\mall\model\NotificationModel;

class PageController extends HomeBaseController
{
    /**
     * 页面管理
     * @return mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function index()
    {
        $postService = new PostService();
        $pageId      = $this->request->param('id', 0, 'intval');
        $page        = $postService->publishedPage($pageId);

        if (empty($page)) {
            abort(404, ' 页面不存在!');
        }

        $this->assign('page', $page);

        $more = $page['more'];

        $tplName = empty($more['template']) ? 'page' : $more['template'];

        return $this->fetch("/$tplName");
    }

    /**
     * 文旅详情
     * @return mixed
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function culture()
    {        
        $cultureModel = new CultureModel();
        $cultureId  = $this->request->param('id', 0, 'intval');
        $culture = $cultureModel->where('id', $cultureId)->find();

        if (empty($culture)) {
            abort(404, '文旅不存在!');
        }

        $culture['post_title'] = $culture['name'];
        $culture['post_content'] = $culture['introduction'];
        $this->assign('page', $culture);

        $tplName = 'page';
        return $this->fetch("/$tplName");
    }

    /**
     * 通知详情
     * @return mixed
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function notification()
    {        
        $notificationModel = new NotificationModel();
        $notificationId  = $this->request->param('id', 0, 'intval');
        $notification = $notificationModel->where('id', $notificationId)->find();

        if (empty($notification)) {
            abort(404, '通知不存在!');
        }

        $notification['post_title'] = $notification['title'];
        $notification['post_content'] = $notification['detail'];
        $this->assign('page', $notification);

        $tplName = 'page';
        return $this->fetch("/$tplName");
    }

    /**
     * 推广注册
     * @return mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function promote()
    {
        $parentId = $this->request->param('parent_id', 0, 'intval');
        $this->assign('parent_id', $parentId);

        $userModel = new UserModel();
        $user = $userModel->where('id', $parentId)->find();
        if (empty($user)) {
            $this->error('用户不存在');
        }
        $this->assign('parent_nick_name', $user['user_nickname']);

        return $this->fetch("/promote");
    }
    
    /**
     * 安装包下载
     * @return mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function download()
    {
        return $this->fetch("/download");
    }
}
